﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyMovieSystem
{
    public partial class FrmBuy : Form
    {
        public FrmBuy()
        {
            InitializeComponent();
        }
       
        //加载事件
        private void FrmBuy_Load(object sender, EventArgs e)
        {
            InitialForm();
            InitialSeat();
            DrawNumber();
        }

        /// <summary>
        /// 座位图
        /// </summary>
        public Image SeatBoard = new Bitmap(490, 270);
        /// <summary>
        /// 放映厅
        /// </summary>
        public Hall hall = new Hall();
        /// <summary>
        /// 影片
        /// </summary>
        public Movie movie=new Movie();
        /// <summary>
        /// 统计座位
        /// </summary>
        int countSeat = 0;
        /// <summary>
        /// 存储选中的座位
        /// </summary>
        List<Seat> checkedSeat = new List<Seat>();

        #region 初始化信息
        /// <summary>
        /// 初始化控件
        /// </summary>
        private void InitialForm()
        {
            label13.Parent = pictureBox8;
            label1.Text = hall.H_Name;
            lblMName.Text = movie.MovieName;
            lblType.Text = movie.MovieType;
            lblDuration.Text = movie.MovieDuration;
            lblHallName.Text = hall.H_Name;
            lblTime.Text = hall.ShowData.ToShortDateString() +"(" + hall.ShowTime +")";
            lblPrice.Text = movie.MoviePrice + " * " + countSeat;
            lblSeatInfo.Text = "已选择" + countSeat + "个座位";
        }

        /// <summary>
        /// 初始化影片
        /// </summary>
        private void InitialSeat()
        {
        
            for (int i = 0; i < hall.SeatInfo.GetLength(0); i++)
            {
                for (int j = 0; j < hall.SeatInfo.GetLength(1); j++)
                {
                    if (hall.SeatInfo[i, j] == null)
                    {
                        hall.SeatInfo[i, j] =
                            new Seat(i.ToString() + j, j + 1, i + 1, new Point(15 + i * 35, j * 30), 0);
                    }
                }
            }
            DrawSeat();
        }
        #endregion

        #region 画座位
        /// <summary>
        /// 画座位
        /// </summary>
        public void DrawSeat()
        {
            JudgeHallAndSeat();

            for (int i = 0; i < hall.SeatInfo.GetLength(0); i++)
            {
                for (int j = 0; j < hall.SeatInfo.GetLength(1); j++)
                {
                    if (hall.SeatInfo[i, j] != null)
                    {
                        //把当前座位画在座位图上
                        Graphics g = Graphics.FromImage(SeatBoard);
                        //把座位图画上去
                        g.DrawImage(hall.SeatInfo[i, j].SeatImag, hall.SeatInfo[i, j].SeatPoint);
                    }
                }
            }
            pictureBox4.Image = SeatBoard;
        }
        #endregion

        #region 判断影厅和座位
        /// <summary>
        /// 判断影厅和座位
        /// </summary>
        private void JudgeHallAndSeat()
        {
            if (hall.H_ID == "004")
            {
                for (int i = 4; i <= 8; i++)
                {
                    for (int j = 0; j <= 1; j++)
                    {
                        hall.SeatInfo[i, j].IsEmpty = 3;
                        hall.SeatInfo[i, j].SeatImag = Image.FromFile(@"image/zwnull.png");
                    }
                }
            }
            else if (hall.H_ID == "001")
            {
                for (int i = 5; i <= 7; i++)
                {
                    for (int j = 3; j <= 4; j++)
                    {
                        hall.SeatInfo[i, j].IsEmpty = 3;
                        hall.SeatInfo[i, j].SeatImag = Image.FromFile(@"image/zwnull.png");
                    }
                }
            }

        }
        #endregion

        #region 画序号
        public void DrawNumber()
        {
            Image Board = new Bitmap(30, 270);
            for (int i = 0; i < hall.SeatInfo.GetLength(1); i++)
            {
                Graphics g = Graphics.FromImage(Board);
                //把背景图画上去
                g.DrawImage(Image.FromFile(@"image//xuhao.png"), new Point(0, i * 30));
                //画字
                Font font = new Font("宋体", 12);
                Brush brush = new SolidBrush(Color.White);
                g.DrawString((i+1).ToString(), font, brush, new Point(2, 2+i * 30));
            }
            pictureBox5.Image = Board;
        }
        #endregion

        #region  单击pictureBox发生
        //单击pictureBox发生
        private void pictureBox4_MouseClick(object sender, MouseEventArgs e)
        {
            try
            {
                int y = (e.X - 15) / 35;
                int x = (e.Y) / 30;
                //判断座位状态为空 则改为选中状态
                if (hall.SeatInfo[y, x].IsEmpty == 0)
                {
                    if (countSeat == 4)
                    {
                        MessageBox.Show("一次最多可选4个座位", "提示：");
                        return;
                    }
                    hall.SeatInfo[y, x].SeatImag = Image.FromFile("image//xuanzhong.png");
                    hall.SeatInfo[y, x].IsEmpty = 2;
                    //选中的座位存储至集合
                    checkedSeat.Add(hall.SeatInfo[y, x]);
                    countSeat++; //统计累加

                }
                //判断座位状态为选中状态 则改为选空
                else if (hall.SeatInfo[y, x].IsEmpty == 2)
                {
                    hall.SeatInfo[y, x].SeatImag = Image.FromFile("image//null.png");
                    countSeat--; //递减
                    hall.SeatInfo[y, x].IsEmpty = 0;
                    //选中的座位从集合移除
                    checkedSeat.Remove(hall.SeatInfo[y, x]);
                }
                //判断座位状态为已售状态
                else if (hall.SeatInfo[y, x].IsEmpty == 1)
                {
                    MessageBox.Show("该座位已售出！");
                }
                lblSeat01.Text = "";
                lblSeat02.Text = "";
                lblSeat03.Text = "";
                lblSeat04.Text = "";

                for (int i = 0; i < checkedSeat.Count; i++)
                {
                    if (i == 0)
                        lblSeat01.Text = checkedSeat[i].Row + "排"
                            + checkedSeat[i].Column + "列";
                    else if (i == 1)
                        lblSeat02.Text = checkedSeat[i].Row + "排"
                            + checkedSeat[i].Column + "列";
                    else if (i == 2)
                        lblSeat03.Text = checkedSeat[i].Row + "排"
                            + checkedSeat[i].Column + "列";
                    else if (i == 3)
                        lblSeat04.Text = checkedSeat[i].Row + "排"
                            + checkedSeat[i].Column + "列";
                }
                lblPrice.Text = movie.MoviePrice + " * " + countSeat;
                lblSeatInfo.Text = "已选择 " + countSeat + " 个座位";
                lblSumPrice.Text = countSeat * movie.MoviePrice + "";
                DrawSeat();
            }
            catch
            {  }
        }
        #endregion

        #region 确认订单
        //确认下单
        private void button2_Click(object sender, EventArgs e)
        {
            if (countSeat == 0)
            {
                MessageBox.Show("请选择座位","提示：");
                return;
            }
            string format=string.Format("购票数量：{0}\n金额小计：{1}元",countSeat,movie.MoviePrice*countSeat);
            DialogResult  r = MessageBox.Show(format,"确认订单：",MessageBoxButtons.OKCancel,MessageBoxIcon.Information);
            if (DialogResult.OK != r)
                return;
            foreach (Seat item in checkedSeat)
            {
                hall.SeatInfo[item.Column - 1, item.Row - 1].IsEmpty = 1;
                hall.SeatInfo[item.Column - 1, item.Row - 1].SeatImag 
                    = Image.FromFile("image//yishou.png");
                Ticket t = new Ticket
                    (TicketManage.listTick.Count+1, hall.SeatInfo[item.Column - 1, item.Row - 1], movie, hall);
                //将购买的票信息添加至已售的票集合中                
                TicketManage.listTick.Add(t);
            }
            if (RoleManage.user != null)
            {
                //将购买的影片添加至用户看过的影片集合中
                RoleManage.user.LookOffMovie.Add(movie);
            }
            checkedSeat.Clear();
            countSeat = 0;
            lblSeat01.Text = "";
            lblSeat02.Text = "";
            lblSeat03.Text = "";
            lblSeat04.Text = "";
            lblPrice.Text = movie.MoviePrice + " * " + countSeat;
            lblSeatInfo.Text = "已选择" + countSeat + "个座位";
            lblSumPrice.Text = countSeat * movie.MoviePrice + "";
            DrawSeat();
            Random ran = new Random();
            int num = ran.Next(10, 100) * 100;
            MessageBox.Show("购票成功!\n订单号："+num,"提示：");
            TicketManage.SerializeTicket();
            MovieManage.SerializeMovie();
        }
        #endregion

        #region 窗体关闭事件
        private void FrmBuy_FormClosing(object sender, FormClosingEventArgs e)
        {
            foreach (Seat  item in checkedSeat)
            {
                if (item.IsEmpty == 2)
                {
                    DialogResult r = MessageBox.Show("已选中座位，确定退出吗？", "提示：",
                        MessageBoxButtons.OKCancel,MessageBoxIcon.Information);
                    if (r != DialogResult.OK)
                    {
                        //是否取消事件
                        e.Cancel = true ;
                        return;
                    }
                    break;
                }
            }
            foreach (Seat item in checkedSeat)
            {
                if (item.IsEmpty == 2)
                {
                    hall.SeatInfo[item.Column - 1, item.Row - 1].IsEmpty = 0;
                    hall.SeatInfo[item.Column - 1, item.Row - 1].SeatImag = Image.FromFile("image//null.png");
                }
            }
            
            checkedSeat.Clear();
            DrawSeat();
        }
        #endregion

        private void pictureBox8_Click(object sender, EventArgs e)
        {

        }
    }
}
